package com.rpframework.utils

import java.io.*
import java.util.*

/**
 * 快速创建文件TXT
 */
fun File.fast(content: String): File {
    val temp = File(System.getProperty("user.dir") + "${File.separator}${UUID.randomUUID()}")
    val ps = PrintStream(FileOutputStream(temp))
    ps.append(content)
    ps.close()
    return temp
}

/**
 * 文件工具
 */
object FileCommon {
    fun tempFile(content: String? = null, handle: FileFast) {
        val file = File(System.getProperty("user.dir") + "${File.separator}${UUID.randomUUID()}")
        content?.let {
            val out = FileOutputStream(file)
            val rd = BufferedWriter(OutputStreamWriter(out, "utf-8"))
            rd.write(content);
            rd.close()
            out.close()
        }
        try {
            handle.handle(file)
        } finally {
            file.delete()
        }
    }
}

interface FileFast {
    fun handle(temp: File)
}